//
//  ViewController.m
//  UILession_HomeWork
//
//  Created by 蒋杏飞 on 15/9/25.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ViewController.h"
#import "HeaderView.h"
#import "ListModel.h"
#import "StyleTableViewCell.h"
#import "NormalTableViewCell.h"
#import "ImageTableViewCell.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic, retain) UITableView *table;

@end

@implementation ViewController

- (void)dealloc
{
    [_tableArray release];
    [_table release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableArray = [NSMutableArray array];
    
    [self createTable];
    [self receiveData];
}

// 分为tableheaderview，cell
// 1.先创建header，
// 2.自定义cell
#pragma mark -
#pragma mark 创建table
- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20) style:UITableViewStylePlain];
    [self.table registerClass:[StyleTableViewCell class] forCellReuseIdentifier:@"cellStyle"];
    [self.table registerClass:[NormalTableViewCell class] forCellReuseIdentifier:@"cellNormal"];
    [self.table registerClass:[ImageTableViewCell class] forCellReuseIdentifier:@"cellImages"];
    
    self.table.rowHeight = 80;
    [_table setDelegate:self];
    [_table setDataSource:self];
    [self.view addSubview:_table];
    [_table release];
    
    // 创建headerview添加到tableview上
    _table.tableHeaderView = [[[HeaderView alloc] initWithFrame:CGRectMake(0, 0, _table.frame.size.width, 200)] autorelease];
}

#pragma mark - 
#pragma mark 获得数据
- (void)receiveData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"YiChe" ofType:@"plist"];
    
    NSDictionary *root = [NSDictionary dictionaryWithContentsOfFile:path];
    NSDictionary *dataDic = [root objectForKey:@"data"];
    NSArray *array = [dataDic objectForKey:@"list"];
    
    for (NSDictionary *dic in array) {
        ListModel *model = [[ListModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.tableArray addObject:model];
        [model release];
    }
    // 刷新界面
    [_table reloadData];
    [self setScrollValue];
}
- (void)setScrollValue
{
    HeaderView *header = (HeaderView *)self.table.tableHeaderView;
    [header setImageWithModels:self.tableArray];
}



#pragma mark -
#pragma mark tableview 协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = ((int)_tableArray.count - 4) >= 0 ? [_tableArray count] - 4 : 0;
    
    return count + 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        StyleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellStyle"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    
    ListModel *model = [_tableArray objectAtIndex:indexPath.row + 3];
    if ([model.type integerValue] == 3) {// 三张图片
        
        ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellImages"];
        
        [cell setvaluesWithModel:model];
        
        return cell;
    } else {
        NormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellNormal"];
        NSString *url = model.picCover;
        url = [url stringByReplacingOccurrencesOfString:@"{0}" withString:@"100"];
        url = [url stringByReplacingOccurrencesOfString:@"{1}" withString:@"100"];
        model.picCover = url;
        [cell setValuesWithModel:model];
        
        return cell;
    }
    
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 100;
    }
    return 80;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



@end






